﻿package com.zmcn.msg.business.phoneGroup.action;

import java.io.File;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.ServletRequestUtils;

import com.zmcn.msg.bean.vo.PhoneVO;
import com.zmcn.msg.bean.vo.UserVO;
import com.zmcn.msg.business.phoneGroup.service.PhoneGroupService;
import com.zmcn.msg.business.phoneGroup.vo.PhoneGroupVO;
import com.zmcn.msg.fw.action.BaseAction;
import com.zmcn.msg.fw.action.Page;
import com.zmcn.msg.fw.util.CodeUtil;
import com.zmcn.msg.fw.util.OutUtil;
import com.zmcn.msg.pub.Const;

/**
 * 
 * 分群信息Action控制层
 * 
 * @author <b>欧建东<br/>
 *         Email:oyjd@163.com</b>
 * @version 1.0
 */
@Action(value = "/phoneGroup")
@Results({ @Result(name = "viewImp", location = Const.JSP_phoneGroup_phone_imp), @Result(name = "viewList", location = Const.JSP_phoneGroup_list),
		@Result(name = "viewPhoneList", location = Const.JSP_phoneGroup_phone_list), @Result(name = "viewFrontGroup", location = Const.JSP_front_phoneGroup) })
public class PhoneGroupAction extends BaseAction {

	/**
	 * 
	 */
	private static final long serialVersionUID = 45354347027015039L;

	private static final Log log = LogFactory.getLog(PhoneGroupAction.class);
	private PhoneGroupService service;

	@Autowired
	public void setService(PhoneGroupService service) {
		this.service = service;
	}

	/**
	 * 进入前台消息组列表页面
	 * 
	 * @return
	 */
	public String viewFrontGroup() {
		try {
			HttpServletRequest req = getRequest();
			String batchId = ServletRequestUtils.getStringParameter(req, "batchId");
			String msgId = ServletRequestUtils.getStringParameter(req, "msgId");
			Page<PhoneGroupVO> page = new Page<PhoneGroupVO>(req);
			page.setPage(1);
			page.setPageSize(Integer.MAX_VALUE);
			UserVO userVO = (UserVO) session.get(CodeUtil.SessionKey);
			page.getParas().put("userId", new String[] { userVO.getUserId() });
			service.getList(page);
			req.setAttribute("batchId", batchId);
			req.setAttribute("msgId", msgId);
			req.setAttribute("data", page.getResult());
		} catch (Exception err) {
			log.error(err.getMessage(), err);
		}
		return "viewFrontGroup";
	}

	/**
	 * 进入分群信息列表页面
	 * 
	 * @return
	 */
	public String viewList() {
		return "viewList";
	}

	/**
	 * 进入特定群手机号码信息
	 * 
	 * @return
	 */
	public String viewPhoneList() {
		return "viewPhoneList";
	}

	/**
	 * 进入导入界面
	 * 
	 * @return
	 */
	public String viewImp() {
		return "viewImp";
	}

	/**
	 * 获取特定群手机号码信息
	 * 
	 * @return
	 */
	public String getList() {
		Map<String, Object> map = CodeUtil.getJsonMap();
		try {
			HttpServletRequest req = getRequest();
			Page<PhoneGroupVO> page = new Page<PhoneGroupVO>(req);
			UserVO userVO = (UserVO) session.get(CodeUtil.SessionKey);
			page.getParas().put("userId", new String[] { userVO.getUserId() });
			service.getList(page);
			//
			map.put("count", page.getCount());
			map.put("info", page.getResult());
			map.put("code", CodeUtil.SUCCESS);
		} catch (Exception err) {
			log.error(err.getMessage(), err);
			map.put("code", CodeUtil.UNKOWN_ERR);
		}
		OutUtil.outJson(map, getResponse());
		return null;
	}

	/**
	 * 获取特定群手机号码信息
	 * 
	 * @return
	 */
	public String getPhoneList() {
		Map<String, Object> map = CodeUtil.getJsonMap();
		try {
			HttpServletRequest req = getRequest();
			Page<PhoneVO> page = service.getPhoneList(new Page<PhoneVO>(req));
			//
			map.put("count", page.getCount());
			map.put("info", page.getResult());
			map.put("code", CodeUtil.SUCCESS);
		} catch (Exception err) {
			log.error(err.getMessage(), err);
			map.put("code", CodeUtil.UNKOWN_ERR);
		}
		OutUtil.outJson(map, getResponse());
		return null;
	}

	/**
	 * 导入数据
	 * 
	 * @return
	 */
	public String imp() {
		Map<String, Object> map = CodeUtil.getJsonMap();
		try {
			if (service.imp(fileFileName, file)) {
				map.put("code", CodeUtil.SUCCESS);
			}
		} catch (Exception err) {
			log.error(err.getMessage(), err);
			map.put("code", CodeUtil.UNKOWN_ERR);
		}
		OutUtil.outJson(map, getResponse());
		return null;
	}

	// 上传文件域对象
	private File file;
	// 上传文件名
	private String fileFileName;

	public void setFile(File file) {
		this.file = file;
	}

	public void setFileFileName(String fileFileName) {
		this.fileFileName = fileFileName;
	}

}
